package com.linq.cool.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linq.cool.system.domain.SysDictItem;
import com.linq.cool.system.dto.SysDictItemQueryDTO;
import com.linq.cool.system.mapper.SysDictItemMapper;
import com.linq.cool.system.service.SysDictItemService;
import com.linq.cool.system.utils.DictUtils;
import com.linq.cool.system.vo.SysDictItemVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2020/12/29 19:39
 * @Description:
 * @Version: 1.0.0
 */
@Service

public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {
    @Override
    public IPage<SysDictItemVO> listByPage(IPage<SysDictItemVO> page, SysDictItemQueryDTO dto) {
        return baseMapper.listByPage(page, dto);
    }

    @Override
    public boolean saveDictItem(SysDictItem dictItem) {
        if (this.save(dictItem)) {
            // 清空缓存
            DictUtils.clearDictCache();
            return true;
        }
        return false;
    }

    @Override
    public boolean updateDictItem(SysDictItem dictItem) {
        if (this.updateById(dictItem)) {
            // 清空缓存
            DictUtils.clearDictCache();
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByIds(List<Long> dictItemIds) {
        if (this.removeByIds(dictItemIds)) {
            DictUtils.clearDictCache();
            return true;
        }
        return false;
    }


    private LambdaQueryWrapper<SysDictItem> getLambdaQueryWrapper(SysDictItem dictItem) {
        return new LambdaQueryWrapper<SysDictItem>()
                .like(StringUtils.checkValNotNull(dictItem.getLabel()), SysDictItem::getLabel, dictItem.getLabel())
                .eq(StringUtils.checkValNotNull(dictItem.getStatus()), SysDictItem::getStatus, dictItem.getStatus());
    }
}
